$(function () {
    $.extend({//随机添加怪物;
        addMonster: function () {
            var random = Math.round(Math.random() * 10);
            if (random >= 9) {
                $(".wrap").append($("<div class='monster'></div>"));
                var monsterLength = $(".monster").length;
                var addWhenMonster = Math.random() * 9;
                if (addWhenMonster >= 0 && addWhenMonster < 1.5) {
                    $(".monster:eq(" + (monsterLength - 1) + ")").attr({ type: "蘑菇", idx: (monsterLength - 1) }).css({
                        bottom: 80,
                        left: 800,
                        background: "url('./images/怪物_蘑菇.png') no-repeat",
                        backgroundSize: "100% 100%"
                    });
                } else if (addWhenMonster > 2 && addWhenMonster < 3.5) {
                    $(".monster:eq(" + (monsterLength - 1) + ")").attr({ type: "王八", idx: (monsterLength - 1) }).css({
                        bottom: 80,
                        left: 800,
                        background: "url('./images/怪物_王八.png') no-repeat",
                        backgroundSize: "100% 100%"
                    });
                } else if (addWhenMonster > 4 && addWhenMonster < 4.5) {
                    $(".monster:eq(" + (monsterLength - 1) + ")").attr({ type: "飞天王八", idx: (monsterLength - 1) }).css({
                        bottom: 80,
                        left: 800,
                        background: "url('./images/怪物_飞天王八.png') no-repeat",
                        backgroundSize: "100% 100%"
                    });
                }
            }
        },//判断怪物是否出了游戏界面，如果是则删除
        removeMonster: function () {
            $(".monster").each(function (idx, item) {
                if (parseInt($(item).css("left")) <= -$(item).width()) {
                    $(item).remove();
                }
            })
        },//怪物自动跑起来;并且随时检测，碰到的是玩家还是障碍物
        moveMonster:function(){
            $(".monster").move = setInterval(function(){
                var monsterStart = parseInt($(".monster").css("left"));
                var peopleLeft = parseInt($(".people").css("left"));
                var monsterBottom = parseInt($(".monster").css("bottom"));
                var peopleBottom = parseInt($(".people").css("bottom"));
                $(".monster").css({left:monsterStart-=6});
                if(monsterStart<=peopleLeft+$(".people").width() && peopleBottom<=monsterBottom+$(".monster").height() && monsterStart >= peopleLeft){
                    alert("游戏结束");
                }
            },30)
        }
    })
})